package server

import (
	"errors"
	"go.manyji.com/im3/app/api"
	"go.manyji.com/im3/event"
	"go.manyji.com/lib/app"
	"go.manyji.com/lib/config"
	"go.manyji.com/lib/log"
	"go.manyji.com/lib/timesched"
	"sort"
	"time"
)

var (
	fDid          = config.Int("im.dept", 0, "关联IM部门ID")
	fSyncInterval = config.Int("im.sync.interval", 0, "主动拉取用户数据周期，单位分")
	fSyncSched    = config.String("im.sync.sched", "", "主动拉取用户数据计划时间")
	fAdmin        = config.Ints("server.admin", []int{}, "管理员")
	deptUserList  = []*api.DeptUser{}
	deptUserMap   = map[int]*api.DeptUser{}

	chLoadUsers = make(chan bool)
)

func init() {
	app.Async(loopLoadUsers)
	event.Listen(&event_UsersUpdate{})
	if interval := *fSyncInterval; interval > 0 {
		app.Async(func() {
			for {
				reloadUsers()
				time.Sleep(time.Minute * time.Duration(interval))
			}
		})
	}
	if sched := *fSyncSched; sched != "" {
		if sch, err := timesched.Parse(sched); err != nil {
			log.Err(err)
		} else {
			app.Async(func() {
				sch.Start(func(t time.Time) {
					reloadUsers()
				})
			})
		}
	}
}

type event_UsersUpdate struct{}

func (e *event_UsersUpdate) Handle(from string) {
	reloadUsers()
}

func loopLoadUsers() {
	for <-chLoadUsers {
		if err := loadUsers(); err != nil {
			log.Err(err)
		}
	}
}

func reloadUsers() {
	chLoadUsers <- true
}

func loadUsers() error {
	log.Info("loadUsers")
	list, err := api.GetDeptUserList(*fDid)
	if err != nil {
		return err
	}
	deptUserList = list

	m := map[int]*api.DeptUser{}
	for _, u := range list {
		m[u.Uid] = u
		if _, ok := rolePerm[u.Title]; !ok {
			addRole(u.Title)
		}
	}
	deptUserMap = m
	if fn := onLoadUser; fn != nil {
		app.Async(fn)
	}
	return nil
}

var onLoadUser func()

func OnSyncUsers(fn func()) {
	onLoadUser = fn
}

func isAdmin(uid int) bool {
	for _, i := range *fAdmin {
		if uid == i {
			return true
		}
	}
	return false
}

//func checkUserPerm(uid int, perm string) bool {
//	if isAdmin(uid) {
//		return true
//	}
//	u, ok := deptUserMap[uid]
//	if !ok {
//		return false
//	}
//
//	perms, ok := rolePerm[u.Title]
//	if !ok {
//		return false
//	}
//	return perms[perm]
//}

func checkUserPerm(uid int, perm string) bool {
	if isAdmin(uid) {
		return true
	}
	_, ok := deptUserMap[uid]
	if !ok {
		return false
	}

	return true

	//perms, ok := rolePerm[u.Title]
	//if !ok {
	//	return false
	//}
	//return perms[perm]
}

type User struct {
	Uid    int
	Name   string
	Title  string `json:",omitempty"`
	Leader int
}

func (u *User) HasPermission(perm string) bool {
	return checkUserPerm(u.Uid, perm)
}

func GetUsers() []*User {
	var list []*User
	for _, row := range deptUserList {
		list = append(list, &User{
			Uid:    row.Uid,
			Name:   row.Name,
			Leader: row.Leader,
		})
	}
	return list
}

type UserTreeNode struct {
	User
	Followers []*UserTreeNode
}

func GetUserTree() (*UserTreeNode, error) {
	var tree *UserTreeNode
	m := map[int]*UserTreeNode{}
	for _, du := range deptUserList {
		m[du.Uid] = &UserTreeNode{User: User{Uid: du.Uid, Name: du.Name, Title: du.Title, Leader: du.Leader}, Followers: []*UserTreeNode{}}
	}
	for _, du := range m {
		if du.Leader == 0 {
			if tree != nil {
				return nil, errors.New("部门数据错误：多个负责人")
			}
			tree = du
		} else {
			if l, ok := m[du.Leader]; ok {
				l.Followers = append(l.Followers, du)
			}
		}
	}
	if tree == nil {
		return nil, errors.New("部门数据错误：无负责人")
	}
	tree.Sort()
	return tree, nil
}

func (du *UserTreeNode) Sort() {
	if du.Followers == nil || len(du.Followers) == 0 {
		return
	}
	sort.Slice(du.Followers, func(i, j int) bool {
		return du.Followers[i].Uid < du.Followers[j].Uid
	})
	for _, f := range du.Followers {
		f.Sort()
	}
}

func (du *UserTreeNode) find(uid int) *UserTreeNode {
	if du.Uid == uid {
		return du
	}
	for _, f := range du.Followers {
		if u := f.find(uid); u != nil {
			return u
		}
	}
	return nil
}

func GetUserTreeByLeader(uid int) (*UserTreeNode, error) {
	tree, err := GetUserTree()
	if err != nil {
		return nil, err
	}
	return tree.find(uid), nil
}

func GetUserInfo(uid int) *api.DeptUser {
	u, ok := deptUserMap[uid]
	if !ok {
		return nil
	}
	return u
}
